﻿
using UnityEngine;

namespace SuperScrollView
{

    public class SuperGridView : MonoBehaviour
    {
        SuperListViewItem.OnSelectDelegate OnSelctItem = null;
        SuperListViewItem.OnShowDelegate OnShowItem = null;

        public LoopListView2 mLoopListView;

        public int mItemCountPerRow = 3;

        int mListItemTotalCount = 0;
        public void Init(SuperListViewItem.OnSelectDelegate selctItemFunc, SuperListViewItem.OnShowDelegate showItemFunc, int totalCount = -1)
        {
            OnSelctItem = selctItemFunc;
            OnShowItem = showItemFunc;

            mListItemTotalCount = totalCount;
            int count = mListItemTotalCount / mItemCountPerRow;
            if (mListItemTotalCount % mItemCountPerRow > 0)
            {
                count++;
            }
            mLoopListView.InitListView(count, OnGetItemByIndex);
        }

        public void SetCount(int count, bool resetPos = false)
        {
            SetListItemTotalCount(count, resetPos);
        }

        void SetListItemTotalCount(int count, bool resetPos = false)
        {
            mListItemTotalCount = count;
            if (mListItemTotalCount < 0)
            {
                mListItemTotalCount = 0;
            }
            int count1 = mListItemTotalCount / mItemCountPerRow;
            if (mListItemTotalCount % mItemCountPerRow > 0)
            {
                count1++;
            }
            if (mLoopListView.ItemTotalCount == count1)
                RefreshList();
            else
            {
                mLoopListView.SetListItemCount(count1, resetPos);
                if (!resetPos)
                    RefreshList();
            }

        }

        public void RefreshList()
        {
            mLoopListView.RefreshAllShownItem();
        }

        public void SetScrollEnable(bool isEnable)
        {
            mLoopListView.ScrollRect.enabled = isEnable;
        }

        public void MovePanelToItemIndex(int index, float offset = 0)
        {
            int itemIndex = index;
            if (itemIndex < 0)
            {
                itemIndex = 0;
            }
            itemIndex++;
            int count1 = itemIndex / mItemCountPerRow;
            if (itemIndex % mItemCountPerRow > 0)
            {
                count1++;
            }
            if (count1 > 0)
            {
                count1--;
            }
            mLoopListView.MovePanelToItemIndex(count1, offset);
        }


        LoopListViewItem2 OnGetItemByIndex(LoopListView2 listView, int index)
        {
            if (index < 0)
            {
                return null;
            }
            LoopListViewItem2 item = listView.NewListViewItem("Row");
            item.gameObject.name = "Item_" + index;
            SuperGridViewItem itemScript = item.GetComponent<SuperGridViewItem>();
            if (item.IsInitHandlerCalled == false)
            {
                item.IsInitHandlerCalled = true;
                itemScript.Init(OnSelctItem, OnShowItem);
            }
            for (int i = 0; i < mItemCountPerRow; ++i)
            {
                int itemIndex = index * mItemCountPerRow + i;
                if (itemIndex >= mListItemTotalCount)
                {
                    itemScript.mItemList[i].gameObject.SetActive(false);
                    continue;
                }
                int itemData = itemIndex;

                itemScript.mItemList[i].gameObject.SetActive(true);
                itemScript.mItemList[i].SetItemData(itemIndex);

            }
            return item;
        }

        //todo 之后 需要动态列数的时候再用
        void UpdateItemPrefab()
        {
            ItemPrefabConfData tData = mLoopListView.GetItemPrefabConfData("ItemPrefab");
            GameObject prefabObj = tData.mItemPrefab;
            RectTransform rf = prefabObj.GetComponent<RectTransform>();
            SuperGridViewItem itemScript = prefabObj.GetComponent<SuperGridViewItem>();
            float w = mLoopListView.ViewPortWidth;
            int count = itemScript.mItemList.Count;
            GameObject p0 = itemScript.mItemList[0].gameObject;
            RectTransform rf0 = p0.GetComponent<RectTransform>();
            float w0 = rf0.rect.width;
            int c = Mathf.FloorToInt(w / w0);
            if (c == 0)
            {
                c = 1;
            }
            mItemCountPerRow = c;
            float padding = (w - w0 * c) / (c + 1);
            if (padding < 0)
            {
                padding = 0;
            }
            rf.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, w);
            if (c > count)
            {
                int dif = c - count;
                for (int i = 0; i < dif; ++i)
                {
                    GameObject go = Object.Instantiate(p0, Vector3.zero, Quaternion.identity, rf);
                    RectTransform trf = go.GetComponent<RectTransform>();
                    trf.localScale = Vector3.one;
                    trf.anchoredPosition3D = Vector3.zero;
                    trf.rotation = Quaternion.identity;
                    SuperListViewItem t = go.GetComponent<SuperListViewItem>();
                    itemScript.mItemList.Add(t);
                }
            }
            else if (c < count)
            {
                int dif = count - c;
                for (int i = 0; i < dif; ++i)
                {

                    SuperListViewItem go = itemScript.mItemList[itemScript.mItemList.Count - 1];
                    itemScript.mItemList.RemoveAt(itemScript.mItemList.Count - 1);
                    Object.DestroyImmediate(go.gameObject);
                }
            }
            float curX = padding;
            for (int k = 0; k < itemScript.mItemList.Count; ++k)
            {
                GameObject obj = itemScript.mItemList[k].gameObject;
                obj.GetComponent<RectTransform>().anchoredPosition3D = new Vector3(curX, 0, 0);
                curX = curX + w0 + padding;
            }
            mLoopListView.OnItemPrefabChanged("ItemPrefab");
        }
    }

}

